// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'package:oi_ai_tool/controller/home_page_controller.dart';
import 'package:oi_ai_tool/model/archive.dart';
import 'package:oi_ai_tool/util/string_extention.dart';
import 'package:oi_ai_tool/util/timeUtil.dart';
import 'package:oi_ai_tool/widgets/oi_list_view.dart';

class OiArchive extends StatelessWidget {
  HomePageController homePageController = Get.find<HomePageController>();
  BuildContext? pcontext;
  OiArchive({
    super.key,
    this.pcontext,
  });

  @override
  Widget build(BuildContext context) {
    double searchHeight = 70.sp;
    double bodyHeight = ScreenUtil().screenHeight - searchHeight;
    return SafeArea(
      child: CustomScrollView(slivers: [
        SliverToBoxAdapter(
            child: Container(
          height: searchHeight,
          padding: EdgeInsets.all(20.sp),
          child: TextField(
            scrollPadding: EdgeInsets.zero,
            cursorColor: Colors.cyan.shade300,
            cursorHeight: 16.sp,
            decoration: InputDecoration(
                suffixIcon: Icon(
              Icons.search,
              size: 18.sp,
            )),
          ),
        )),
        SliverToBoxAdapter(
          child: SizedBox(height: bodyHeight.sp, child: historyList(pcontext!)),
        )
      ]),
    );
  }

  Widget historyList(BuildContext buildContext) {
    return OiListView(
        data: homePageController.chatArchive,
        itemBuilder: (context, index) {
          Archive item = homePageController.chatArchive[index];
          return ListTile(
            onTap: () => homePageController.archivetoChat(buildContext, index),
            hoverColor: "#dfdfdf".toColor(),
            focusColor: "#dfdfdf".toColor(),
            trailing: CupertinoButton(
              child: const Icon(Icons.delete),
              onPressed: () => homePageController.archiveDel(index),
            ),
            title: Text(
              TimeUtils.getFormatDate(item.archiveTime!),
              style: TextStyle(
                  fontSize: 12.sp,
                  color: "#636060".toColor(),
                  fontWeight: FontWeight.w600),
            ),
            subtitle: Text(
              item.chatList.isNotEmpty ? item.chatList[0].content ?? "" : "",
              style: TextStyle(
                  fontWeight: FontWeight.bold,
                  fontSize: 14.sp,
                  color: "#15212f".toColor()),
            ),
          );
        });
  }
}
